package com.cskaoyan.wordmemorize.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cskaoyan.wordmemorize.converter.AdminLogConverter;
import com.cskaoyan.wordmemorize.dao.entity.OperateLogDO;
import com.cskaoyan.wordmemorize.dao.mapper.OperateLogMapper;
import com.cskaoyan.wordmemorize.dto.admin.OperateLogDTO;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.request.OperateLogPageRequest;
import com.cskaoyan.wordmemorize.service.AdminLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminLogServiceImpl implements AdminLogService {
    @Autowired
    OperateLogMapper operateLogMapper;
    @Autowired
    AdminLogConverter adminLogConverter;

    // todo:查询系统操作日志
    @Override
    public PageDTO<OperateLogDTO> getOperateLogPage(OperateLogPageRequest request) {

        // todo:判断是否有条件
        LambdaQueryWrapper<OperateLogDO> operateLogQueryWrapper = new LambdaQueryWrapper<>();
        if (request.getEmployeeId() != null) {
            operateLogQueryWrapper.eq(OperateLogDO::getEmployeeId, request.getEmployeeId());
        }
        if (request.getDataType() != null) {
            operateLogQueryWrapper.eq(OperateLogDO::getDataType, request.getDataType());
        }

        // todo:无参时全表查询
        if (request.getPageNum() == null) {
            List<OperateLogDO> operateLogDOs = operateLogMapper.selectList(operateLogQueryWrapper);
            return adminLogConverter.operationLogPage2PageDTO(operateLogDOs, (long) operateLogDOs.size());
        }

        // todo:有参时分页查询
        Page<OperateLogDO> pageParam = new Page<>(request.getPageNum(), request.getCount());
        Page<OperateLogDO> operateLogDOPage = operateLogMapper.selectPage(pageParam, operateLogQueryWrapper);
        return adminLogConverter.operationLogPage2PageDTO(operateLogDOPage.getRecords(), operateLogDOPage.getTotal());

    }
}
